package com.jiajunsong.blog.oauth2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

@Configuration
@EnableAuthorizationServer
public class BlogAuthenticationServerConfig extends AuthorizationServerConfigurerAdapter {
	// public class BlogAuthenticationServerConfig {
	@Autowired
	private AuthenticationManager authenticationManager;
	@Autowired
	private UserDetailsService userDetailsService;

	@Override
	public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
		endpoints.authenticationManager(authenticationManager).userDetailsService(userDetailsService);
	}

	@Override
	public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
		clients.inMemory() // 使用in-memory存储
				.withClient("client").secret("$2a$10$JXErJki5EWqKKnY8F3Z1w.GZuFnvO8zr2rWeByRVhFH5wVvkHdmiO") // client_secret经过BCry加密后的密码，明文是123456
				.authorizedGrantTypes("password", "authorization_code", "refresh_token", "client_credentials","implicit") // 该client允许的授权类型
				.scopes("all")// 允许的授权范围
				.resourceIds("oauth2-resource")
				.accessTokenValiditySeconds(3600)
				.refreshTokenValiditySeconds(50000);
	}

}
